package com.liy.framework.common.orm.transactional;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource;
import org.springframework.transaction.interceptor.RollbackRuleAttribute;
import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
import org.springframework.transaction.interceptor.TransactionAttribute;
import org.springframework.transaction.interceptor.TransactionInterceptor;

/**
* 事务拦截器配置
*/
@Aspect
@Configuration
public class TransactionAdviceInterceptor {

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    /*
    *  事务拦截器
    */
    @Bean
    @Value("${transacation.timeout: 10}")
    public TransactionInterceptor txAdvice(Integer transacationTimeout) {

        // 名称匹配-事务属性源  Name Match Transaction Attribute Source
        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();


        // 基于规则的事务属性  隔离级别 只读
        RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
        readOnlyTx.setReadOnly(true); // 设置只读
        readOnlyTx.setPropagationBehavior(4); // 传播行为


        // 基于规则的事务属性  隔离级别 默认
        RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute();
        requiredTx.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class))); //回滚规制
        requiredTx.setPropagationBehavior(0);  // 传播行为
        requiredTx.setTimeout(transacationTimeout);  // 超时


        Map<String, TransactionAttribute> txMap = new HashMap();

        // 默认
        txMap.put("add*", requiredTx); //
        txMap.put("save*", requiredTx);
        txMap.put("modify*", requiredTx);
        txMap.put("insert*", requiredTx);
        txMap.put("update*", requiredTx);
        txMap.put("del*", requiredTx);
        txMap.put("delete*", requiredTx);

        // 只读
        txMap.put("get*", readOnlyTx);
        txMap.put("query*", readOnlyTx);
        txMap.put("find*", readOnlyTx);
        txMap.put("select*", readOnlyTx);


        source.setNameMap(txMap);

        // 事务管理器 + 规制属性
        // TransactionManager + NameMatchTransactionAttributeSource
        TransactionInterceptor txAdvice = new TransactionInterceptor(this.platformTransactionManager, source);

        return txAdvice;
    }


    /*
    * 执行切点
    */
    @Bean
    @Value("${transacation.expression: execution( * com.liy.*.service.*.*(..))}")
    public Advisor txAdviceAdvisor(String AOP_POINTCUT_EXPRESSION) {
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
        return new DefaultPointcutAdvisor(pointcut, this.txAdvice((Integer)null));
    }
}
